#include "mainwindow.h"
#include <QLabel>
#include <QPlainTextEdit>
#include <QScreen>
#include <QApplication>
#include "SARibbonCategory.h"
#include "SARibbonPannel.h"
#include "SARibbonBar.h"
#include "PluginInterface.h"
#include "MainuiPluginInterface.h"



MainWindow::MainWindow(QWidget *parent)
    : SARibbonMainWindow(parent),
      m_centerAreaWidget(nullptr)
{
    InitPlugins();
    InitRibborDock();
    this->setMinimumSize(1100,700);
    loadStyleSheet(":/Icon/global.qss");

}


void MainWindow::loadStyleSheet(const QString& qsStyleFile)
{
    if (!QFile::exists(qsStyleFile))
        return ;
    QFile qss(qsStyleFile);
    qss.open(QFile::ReadOnly);
    qApp->setStyleSheet(qApp->styleSheet() + qss.readAll());
    qss.close();
}



MainWindow::~MainWindow()
{

}


void MainWindow::InitPlugins()
{
    m_plugManager=PluginManager::get();
    m_plugManager->loadPlugins();
    auto plugins = m_plugManager->pluginList();
    for ( PluginInterface *plugin : plugins )
    {
        if ( plugin == nullptr )
        {
            Q_ASSERT( false );
            continue;
        }

        const QString pluginName = plugin->getName();

        Q_ASSERT( !pluginName.isEmpty() );

        if ( pluginName.isEmpty() )
        {
            // should be unreachable - we have already checked for this in ccPlugins::Find()
            continue;
        }

        switch ( plugin->getType() )
        {
        case MAIN_UI_PLUGIN:
        {
            m_MainUiPlugin = static_cast<MainuiPluginInterface*>(plugin);
            m_MainUiPlugin->setMainAppInterface(this);
            break;
        }
        case UNKN_STD_PLUGIN:
        {
            break;
        }
        case AQCU_SYS_PLUGIN:
            break;
        case PROC_SYS_PLUGIN:
            break;
        case DB_SYS_PLUGIN:
            break;
        }

    }


}



void MainWindow::InitRibborDock()
{
    //创建dock管理器
    m_DockManager = new ads::CDockManager(this);

    //ribbon菜单栏初始化
    SAFramelessHelper* helper = framelessHelper();
    helper->setRubberBandOnResize(false);



    //根据mainuiplugin初始界面
    if(m_MainUiPlugin)
    {
        SARibbonCategory* m_categoryMain =  ribbonBar()->addCategoryPage(m_MainUiPlugin->getCatogry());
        m_categoryMain->setObjectName(m_MainUiPlugin->getCatogry());


        //ribborbar add panel and action
        QMap<QString,QList<QAction *>> actMap=m_MainUiPlugin->getActions();
        QMapIterator<QString,QList<QAction *>> it(actMap);
        while(it.hasNext()){
               it.next();
               SARibbonPannel* Panel = m_categoryMain->addPannel(it.key());
               for(int i=0;i<it.value().size();i++){
                   Panel->addLargeAction(it.value()[i]);
               }
           }


        // dock add widget
        QList<QWidget *> uiwidgetlist=m_MainUiPlugin->getWidgets();
        foreach (QWidget * uiwidget, uiwidgetlist) {
           ads::CDockWidget* DockWidget = new ads::CDockWidget(uiwidget->windowTitle());
           DockWidget->setWidget(uiwidget);
           if(m_centerAreaWidget==nullptr)
                m_centerAreaWidget = m_DockManager->setCentralWidget(DockWidget);
           else
                m_DockManager->addDockWidget(ads::BottomDockWidgetArea, DockWidget);
        }
    }

    if(m_centerAreaWidget)
    {
         QScreen* screen = QApplication::primaryScreen();
         int leftwidth   = screen->size().width() / 6;
         int rightwidth  = leftwidth;
         int centerwidth = screen->size().width() - leftwidth - rightwidth;
         m_DockManager->setSplitterSizes(m_centerAreaWidget, { leftwidth, centerwidth, rightwidth });
    }

}

